﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using  UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.UIElements;

namespace Modules.Guide.Runtime
{
    public class GuideMask : MonoBehaviour, ICanvasRaycastFilter, IPointerDownHandler
    {
        public bool m_enabled = true;
        public bool m_force = true;
        public bool m_downed = false;
        public Transform m_target;
        public Camera m_camera;

        void OnEnable()
        {
            m_enabled = true;
        }

        void OnDisable()
        {
            m_enabled = false;
        }

        public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
        {
            if (m_enabled == false)
                return false;

            if (m_target != null)
            {
                //UI
                if (m_target is RectTransform)
                {
                    m_camera = GameObject.Find("UIRoot/UICamera").GetComponent<Camera>();
                    if (RectTransformUtility.RectangleContainsScreenPoint(m_target as RectTransform, sp, m_camera))
                    {
                        return false;
                    }

                    m_camera = WorldCameraManager.mainCamera;
                    if (RectTransformUtility.RectangleContainsScreenPoint(m_target as RectTransform, sp, m_camera))
                    {
                        return false;
                    }
                }
                else
                {
                    //3D 物体转成屏幕区域
                    //3D 物体射线检测
                    m_camera = WorldCameraManager.mainCamera;
                    var ray = m_camera.ScreenPointToRay(sp);
                    RaycastHit hitInfo;
                    if (Physics.Raycast(ray, out hitInfo, m_camera.farClipPlane))
                    {
                        //Debug.Log($"{hitInfo.collider?.gameObject}");
                        if (hitInfo.collider?.transform == m_target)
                        {
                            return false;
                        }
                    }
                }
            }
            
            
            if (m_force == false)
            {
                if (Input.GetMouseButtonDown(0))
                {
                    m_downed = true;
                    return false;
                }
            }

            return true;
        }
        
        public void OnPointerDown(PointerEventData eventData)
        {
            //if (m_force) return;
            //m_downed = true;
            //PassEvent(eventData, ExecuteEvents.pointerDownHandler);
        }
        
        private GameObject PassEvent<T>(PointerEventData data, ExecuteEvents.EventFunction<T> function) where T : IEventSystemHandler
        {
            List<RaycastResult> results = new List<RaycastResult>();
            EventSystem.current.RaycastAll(data, results); 
            //GameObject current = data.pointerCurrentRaycast.gameObject;
            for(int i = 0; i < results.Count; i++)
            {
                Debug.Log($"--{results[i].gameObject.name}");
                if(results[i].gameObject != gameObject)
                {
                    ExecuteEvents.Execute(results[i].gameObject, data, function);
                    return results[i].gameObject;
                }
            }

            return null;
        }
    }

}
